using UnityEngine;
using System.Collections;

[AddComponentMenu("Third Person Player/Third Person Player Animation")]

public class ThirdPersonPlayerAnimation : MonoBehaviour
{
	public float runSpeedScale = 1.0f;
	public float walkSpeedScale = 1.0f;
	
	void Start()
	{
		//Loop all animations by default
		animation.wrapMode = WrapMode.Loop;
		
		animation["run"].layer = -1;
		animation["walk"].layer = -1;
		animation["idle"].layer = -2;
		animation.SyncLayer(-1);
		
		animation["ledgefall"].layer = 9;
		animation["ledgefall"].wrapMode = WrapMode.Loop;
		
		//The jump animation is clamped an overrides all others
		animation["jump"].layer = 10;
		animation["jump"].wrapMode = WrapMode.ClampForever;
		
		animation["jumpfall"].layer = 10;
		animation["jumpfall"].wrapMode = WrapMode.ClampForever;
		
		//Controlled Descent Animation
		animation["flightjump"].layer = 10;
		animation["flightjump"].wrapMode = WrapMode.ClampForever;
		
		animation["walljump"].layer = 11;
		animation["walljump"].wrapMode = WrapMode.Once;
		
		//Use as a "got hit" animation
		animation["buttstomp"].speed = 0.15f;
		animation["buttstomp"].layer = 20;
		animation["buttstomp"].wrapMode = WrapMode.Once;
		
		AnimationState punch = animation["punch"];
		punch.wrapMode = WrapMode.Once;
		
		//Don't let other animations play when starting
		animation.Stop();
		animation.Play("idle");
	}

	void Update()
	{
		ThirdPersonController playerController = GetComponent<ThirdPersonController>();
		float currentSpeed = playerController.GetSpeed();
		
		//Fade in run
		if(currentSpeed > playerController.walkSpeed)
		{
			animation.CrossFade("run");
			//Fade out jumpland quickly, or the feet will slide
			animation.Blend("jumpland", 0);
		}
		
		//Fade in walk
		else if(currentSpeed > 0.1f)
		{
			animation.CrossFade("walk");
			//Fade out jumpland quickly again
			animation.Blend("jumpland", 0);
		}
		
		//Fade out walk and run
		else
		{
			animation.Blend("walk", 0.0f, 0.3f);
			animation.Blend("run", 0.0f, 0.3f);
		}
		
		animation["run"].normalizedSpeed = runSpeedScale;
		animation["walk"].normalizedSpeed = walkSpeedScale;
		
		if(playerController.IsJumping())
		{
			if(playerController.IsControlledDescent())
			{
				animation.CrossFade("flightjump", 0.2f);
			}
			else if(playerController.HasJumpReachedApex())
			{
				animation.CrossFade("jumpfall", 0.2f);
			}
			else
			{
				animation.CrossFade("jump", 0.2f);
			}
		}	
		
		//Fell down
		else if(!playerController.IsGroundedWithTimeout())
		{
			animation.CrossFade("ledgefall", 0.2f);
		}
		//No longer falling
		else
		{
			animation.Blend("ledgefall", 0.0f, 0.2f);
		}
	}
	
	void DidLand()
	{
		animation.Play("jumpland");
	}
	
	void DidButtStomp()
	{
		animation.CrossFade("buttstomp", 0.1f);
		animation.CrossFadeQueued("jumpland", 0.2f);
	}
	
	public IEnumerator Slam()
	{
		animation.CrossFade("buttstomp", 0.2f);
		ThirdPersonController playerController = GetComponent<ThirdPersonController>();
		
		while(!playerController.IsGrounded())
		{
			yield return null;
		}	
		
		animation.Blend("buttstomp", 0, 0);
	}
	
	void DidWallJump()
	{
	//Wall jump animation played without fade
	//Character controller is rotating 180 degrees
	animation.Play("walljump");
	}
	
}